package m202403.d12;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/3/12 15:50
 */
//746. 使用最小花费爬楼梯
public class Q746 {
    //min(n) = (min(n-1)+c(n-1))||(min(n-2)+c(n-2))
    public int minCostClimbingStairs(int[] cost) {
        return minCostClimbingStairs(cost, cost.length);
    }

//    public int minCostClimbingStairs(int[] cost, int n) {
//        if (n <= 2) {
//            return Math.min(cost[0], cost[1]);
//        }
//        if (n == 3) {
//            return Math.min(cost[0] + cost[2], cost[1]);
//        }
//        return Math.min((minCostClimbingStairs(cost, n - 1) + cost[n - 1]), (minCostClimbingStairs(cost, n - 2) + cost[n - 2]));
//    }

    //[10,15,20,1] n=4
    //[0,0,10,15,
//    public int minCostClimbingStairs(int[] cost, int n) {
//        int[] dp = new int[n + 1];
//        dp[0] = 0;
//        dp[1] = 0;
//        for (int i = 2; i <= n; i++) {
//            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
//        }
//        return dp[n];
//    }

    public int minCostClimbingStairs(int[] cost, int n) {
        int r1 = 0;
        int r2 = 0;
        for (int i = 2; i <= n; i++) {
            int r3 = Math.min(r2 + cost[i - 1], r1 + cost[i - 2]);
            r1 = r2;
            r2 = r3;
        }
        return r2;
    }
}
